sledge.plugs = {}

function sledge.FindPlugs(dir) -- Find any and all plugins recursively
	dir = dir or "sledgeplugs"
	realdir = "../lua/"..dir
	sledge.Debug("Loading plugins from "..dir)
	if(!file.Exists(realdir)) then
		sledge.Debug("Failed to load plugins: "..dir.." doesn't exist")
		return nil
	elseif(!file.IsDir(realdir)) then
		sledge.Debug("Failed to load plugins: "..dir.." isn't a directory")
		return nil
	end
	local list = file.FindInLua(realdir.."/*.lua")
	local loaded = 0
	for k,v in pairs(list) do
		if(file.IsDir(realdir.."/"..v)) then
			sledge.Debug("Recursing into "..dir.."/"..v)
			sledge.FindPlugs(realdir.."/"..v)
		else
			if(!sledge.DisabledPlug(dir.."/"..v)) then
				sledge.PlugFile = dir.."/"..v
				sledge.Debug("Included "..sledge.PlugFile)
				include(dir.."/"..v)
				loaded = loaded+1
			end
		end
	end
	sledge.Debug("Loaded "..loaded.." plugins from "..dir)
end

function sledge.DisabledPlug(file)
	disbaledplugs = sledge.StringToTable(file.Read("sledge/disabledplugs.txt"))
	for k,v in pairs(disabledplugs) do
		if(file == v) then
			return true
		end
	end
	return false
end

function sledge.GetPlugs()
	return sledge.plugs
end

function sledge.LoadPlug(plug) -- Load the plugin
	sledge.Debug("Loading "..plug.File)
	if(sledge.DisabledPlug(plug.File)) then
		plug.Enabled = false
		return false
	end
	if(plug.ClientSide) then
		AddCSLuaFile(plug.File)
	end
	table.insert(sledge.plugs, plug)
	sledge.Debug("Loaded "..plug.File)
end

sledge.FindPlugs(nil)
